<template>
  <div class="login_content">
    <div>
      <el-form ref="rulefrom" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button type="primary" @click="onSubmit" round>登录</el-button>
          <el-button type="info" round @click="resetForm('rulefrom')">重置</el-button>
        </el-form-item>
        <el-checkbox v-model="checked">是否记录状态</el-checkbox>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  props: {},
  data() {
    return {
      checked: true,
      form: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在1到10个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 1, max: 10, message: "长度在1到10个字符", trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  created() {},
  mounted() {},
  watch: {},
  methods: {
    async onSubmit() {
      let { data: res } = await this.$axios.post("login", this.form);
      if (res.meta.status !== 200) {
        return this.$message.error("登录失败");
      } else {
        this.$message.success("登陆成功");
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      }

      console.log(res);
    },
    resetForm (formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    }
  },
  components: {}
};
</script>

<style scoped  lang="less">
.login_content {
  color: red;
  height: 600px;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    margin: 0 auto;
    width: 400px;
    color: red;
    padding: 50px 50px 30px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

    .buttons {
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      button {
        width: 45%;
      }
    }
  }
  .el-checkbox {
    display: block;
    margin-top: 40px ;
    display: flex;
    justify-content: center;
  }
}
</style>
